$(document).ready(function() {
    $.datepicker.setDefaults( $.datepicker.regional[ "nl" ] );
   $('input[name="reservation[dateStart]"]').datepicker({
       showAnim: 'slideDown', numberOfMonths: 3, minDate: 0 ,
       onSelect: function(dateText, inst) { 
        $('input[name="reservation[dateEnd]"]').datepicker("option", "minDate", new Date($(this).datepicker("getDate").getTime() + 1000 * 60 * 60 * 24));
       }});
   $('input[name="reservation[dateEnd]"]').datepicker({showAnim: 'slideDown', numberOfMonths: 3, minDate: 1});

   $('a[rel="calc"]').click(calc);
   $('textarea').elastic();
});


function calc(){ 
    cguests = Number($('select[name="reservation[nbGuests]"]').val());
    crooms = Number($('select[name="reservation[nbRooms]"]').val());
    startd = $('input[name="reservation[dateStart]"]').datepicker("getDate");
    endd = $('input[name="reservation[dateEnd]"]').datepicker("getDate");
    
    if ((startd == null) || (endd == null) || startd >= endd) {
        alert(error_dates);
        return;
    }

    if(((crooms == 2) && (cguests == 1))){
        alert(error_rooms);
        return
    }
    if((crooms == 1) && cguests > 4){
        alert(error_guests);
        return;
    }
    
    days = Math.round((endd - startd) / (1000*60*60*24));
        
    switch (crooms){
       case 1:
       {
	        switch(cguests){
                    case 1:
                        calcsum = days * cp1;
                    break;
                    case 2:
                        calcsum = days * cp2 * 2;
                    break;
                    case 3:
                        calcsum = days *(cp2 * 2+ cp3);
                    break;
                    case 4:
                        calcsum = days *(cp2 * 2 + cp3 + cp4);
                    break;
             }
          }
          case 2:
          {
                switch (cguests){
                    case 2:
                    calcsum = crooms * days * cp1;
                    break;
                    case 3:
                    calcsum = days * (cp2 * 2 + cp1);
                    break;
                    case 4:
                    calcsum = days * cp2 * 2 * crooms; 
                    break;
                    case 5:
                    calcsum = days * (cp2 * 2 * crooms+cp3);
                    break;
                    case 6:
                    calcsum = days * (cp2 + cp3) * 2 * crooms;
                    break;
                    case 7:
                    calcsum = days * ((cp2 * 2 + cp3) * crooms + cp4);
                    break;
                    case 8:
                    calcsum = days * (cp2 * 2 + cp3 + cp4) * crooms;
                    break;  
                }
            }
            }
            if (days == 1)
                calcsum = Math.round(calcsum * (1 + conenight / 100 ));
            else if((startd.getDay() == 1 && endd.getDay() == 5) || (startd.getDay() == 5 && endd.getDay() == 1)){
                calcsum = Math.round( calcsum * (1 - clong / 100));
    }
            
    $(".price").html("&euro; "+calcsum);
}